package com.shenfa.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import com.shenfa.bean.FoldOut;
import com.shenfa.bean.FoldoutProd;
import com.shenfa.bean.FoldoutProdB;
import com.shenfa.service.FoldOutService;

import com.shenfa.util.ResultObject;
import com.shenfa.bean.TreeNode;

/**
 * 插页信息管理Action
 * 
 * @author yanglong
 * 
 * @date 2011-8-24
 * 
 */

@SuppressWarnings("serial")
public class FoldOutAction extends BaseAction {

	@Autowired
	private FoldOutService foldOutService;

	private FoldOut foldOut = new FoldOut();
	private static Logger log=Logger.getLogger(FoldOutAction.class);
	private FoldoutProd fp;
	private FoldoutProdB fpb;

	private List<FoldoutProdB> fpbs;

	public List<FoldoutProdB> getFpbs() {
		return fpbs;
	}

	public void setFpbs(List<FoldoutProdB> fpbs) {
		this.fpbs = fpbs;
	}

	public FoldoutProdB getFpb() {
		return fpb;
	}

	public void setFpb(FoldoutProdB fpb) {
		this.fpb = fpb;
	}

	/** 每页起始数据 */
	private int start;

	/** 每页最大数据 **/
	private int limit;

	public void setStart(int start) {
		this.start = start;
	}

	public void setLimit(int limit) {
		this.limit = limit;
	}

	/**
	 * 查询插页
	 */
	@SuppressWarnings("unchecked")
	public String list() {
		log.debug("start="+start+",limit="+limit+",foldOutpri="+foldOut.getFoldoutpri()+",epibolyNo="+foldOut.getEpibolyno()+",foldOutId="+foldOut.getFoldoutid());
		List<FoldOut> list = foldOutService.getList(foldOut, start, limit);
		result = ResultObject.build("0", list);
		result.put("total", foldOutService.getListCount(foldOut));
		return SUCCESS;
	}

	/**
	 * 添加插页
	 */
	public String add() {
		log.debug("foldOutpri="+foldOut.getFoldoutpri()+",epibolyNo="+foldOut.getEpibolyno()+",foldOutId="+foldOut.getFoldoutid()+",foldOutname="+foldOut.getFoldoutname());
		if (foldOutService.focefoldoutid(foldOut) < 1) {
			int type = foldOutService.add(foldOut);
			if (type == 1) {
				result = ResultObject.build(String.valueOf(type), "添加成功！");
			} else {
				result = ResultObject.build(String.valueOf(type), "操作失败！");
			}
		} else {
			result = ResultObject.build("2","该优先级的插槽已存在！");
		}
		return SUCCESS;
	}

	/**
	 * 修改插页
	 */
	public String upd() {
		log.debug("foldOutpri="+foldOut.getFoldoutpri()+",epibolyNo="+foldOut.getEpibolyno()+",foldOutId="+foldOut.getFoldoutid()+",foldOutname="+foldOut.getFoldoutname());
		int type = foldOutService.upd(foldOut);
		if (type == 1) {
			result = ResultObject.build(String.valueOf(type), "修改成功！");
			//关联修改，修改所有外包商的插槽信息
//			Map<String,Object> map = new HashMap<String,Object>();
//			map.put("foldoutid", foldOut.getFoldoutid());
//			map.put("epibolyno", foldOut.getEpibolyno());
//			map.put("foldoutpri", foldOut.getFoldoutpri());
//			int type2 = foldOutService.updateFoldOut_to_prod(map);
//			if (type2 == 0) {
//				result = ResultObject.build(String.valueOf(type2),
//						"T_S_FOLDOUT_PROD操作失败！");
//			}
		} else {
			result = ResultObject.build(String.valueOf(type), "操作失败！");
		}
		return SUCCESS;
	}

	/**
	 * 删除插页
	 */
	public String del() {
		log.debug("foldOutpri="+foldOut.getFoldoutpri()+",epibolyNo="+foldOut.getEpibolyno()+",foldOutId="+foldOut.getFoldoutid());
		int type = foldOutService.del(foldOut);
		if (type == 1) {
			result = ResultObject.build(String.valueOf(type), "删除成功！");
			//关联删除，删除所有该外包商的插槽信息
//			Map<String,Object> map = new HashMap<String,Object>();
//			map.put("foldoutid", foldOut.getFoldoutid());
//			map.put("epibolyno", foldOut.getEpibolyno());
//			map.put("foldoutpri", foldOut.getFoldoutpri());
//			int type2 = foldOutService.delFoldOut_to_prod(map);
//			if (type2 == 0) {
//				result = ResultObject.build(String.valueOf(type2),
//						"T_S_FOLDOUT_PROD操作失败！");
//			}
		} else {
			result = ResultObject.build(String.valueOf(type), "操作失败！");
		}
		return SUCCESS;
	}

	/**
	 * 根据卡号查询插页详细信息
	 * 
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String listByCard() {
		log.debug("start="+start+",limit="+limit+",period="+fp.getPeriod()+",businpnt="+fp.getBusinpnt_no()+",prodNo="+fp.getProd_no());
		if ("".equals(fp.getPeriod())) {
			fp.setPeriod(null);
		}
		if ("".equals(fp.getBusinpnt_no()) || "all".equals(fp.getBusinpnt_no())) {
			fp.setBusinpnt_no(null);
		}
		List<FoldoutProd> list = foldOutService.listByCard(fp, start, limit);
		result = ResultObject.build("0", list);
		result.put("total", foldOutService.queryCountlbc(fp));
		return SUCCESS;
	}

	/**
	 * 查询个性化插槽详情
	 * 
	 * @return
	 */
	public String listFoldoutDetails() {
		log.debug("id="+fp.getId()+",businpntNo="+fp.getBusinpnt_no());
		List<FoldoutProdB> list = new ArrayList<FoldoutProdB>();
		FoldoutProdB fpb =null;
		if (null == fp.getId() || "".equals(fp.getId())) {			
			FoldOut f=new FoldOut();
			f.setEpibolyno(fp.getBusinpnt_no());
			List<FoldOut> list2 = foldOutService.getList(f, 0, 10000);
			for (FoldOut foldOut : list2) {
				fpb = new FoldoutProdB();
				fpb.setFoldout_index("" + foldOut.getFoldoutid());
				fpb.setFoldout_pri("" + foldOut.getFoldoutpri());
				list.add(fpb);
			}
		} else {
			list = foldOutService.listFoldoutDetails(fp);			
		}
		result = ResultObject.build("0", list);
		return SUCCESS;
	}

	/**
	 * 查询个性化插页所有账单日
	 * 
	 * @param cardid
	 * @return
	 */
	public String listFoldoutperiod() {
		log.debug("start="+start+",limit="+limit);
		List<FoldoutProd> list = foldOutService.listFoldoutperiod(start, limit);
		result = ResultObject.build("0", list);
		return SUCCESS;
	}

	public FoldOut getFoldOut() {
		return foldOut;
	}

	public void setFoldOut(FoldOut foldOut) {
		this.foldOut = foldOut;
	}

	public FoldoutProd getFp() {
		return fp;
	}

	public void setFp(FoldoutProd fp) {
		this.fp = fp;
	}

	public String listEpiboly() {
		List<TreeNode> list = foldOutService.listEpiboly();
		result = ResultObject.build("0", list);
		return SUCCESS;
	}
	// 删除插页与产品关系表从表
	public String deltsfp() {
		log.debug(fpb.toString());
		int type = foldOutService.deltsfp(fpb);
		if (type == 1) {
			result = ResultObject.build(String.valueOf(type), "删除成功！");

		} else {
			result = ResultObject.build(String.valueOf(type), "操作失败！");
		}
		return SUCCESS;
	}

	// 删除插页与产品关系表主表
	public String deltsfps() {
		log.debug("i_id="+fp.getId());
		int type = foldOutService.delete_tsfps(fp);
		if (type == 1) {
			result = ResultObject.build(String.valueOf(type), "删除成功！");

		} else {
			result = ResultObject.build(String.valueOf(type), "操作失败！");
		}
		return SUCCESS;
	}

	// 验证账期时间的合法性 并且增加一条新的插页与产品关系表主表
	public String addfocedate() {
		log.debug(fp.toString());
		if (null!=fp.getPeriod_begen()) {
			String s = fp.getPeriod_begen();
			fp.setPeriod_begen(s.substring(0, 7));
		}
		if (null!=fp.getPeriod_end()) {
			String s = fp.getPeriod_end();
			fp.setPeriod_end(s.substring(0, 7));
		}
		//获取账单日
		String period=fp.getPeriod();
		//如果账单日不存在
		if(null==period|| "".equals(period)){
			result = ResultObject.build("0", "账单日不能为空！");
			return SUCCESS;
		}
		String[] ps=period.split(",");
		//验证时间是否合法
		int type = foldOutService.focedate(fp);
		int type2=0;
		int type3=0;
		if (type < 1) {
			for(String s:ps){
				fp.setPeriod(s);//设置账单日
				type2 = foldOutService.add_tsfps(fp);
				if (type2 == 1) {
					if(null!=fpbs){
						type3= foldOutService.addfoldpb(fpbs,fp);
						if (type3 == 1) {
						} else {
							log.debug("从表操作失败！");
						}
					}
				}
			}
			result = ResultObject.build("1","添加成功！");
		} else {
			result = ResultObject.build("0", "操作失误，您输入的时间有错误!请检查该外包商此账单日下,其他的开始月和截止月");
		}
		return SUCCESS;
	}

	// 验证账期时间的合法性 并且修改一条新的插页与产品关系表主表
	public String updfocedate() {
		log.debug(fp.toString());
		if (null!=fp.getPeriod_begen()) {
			String s = fp.getPeriod_begen();
			fp.setPeriod_begen(s.substring(0, 7));
		}
		if (null!=fp.getPeriod_end()) {
			String s = fp.getPeriod_end();
			fp.setPeriod_end(s.substring(0, 7));
		}
		int type = foldOutService.focedate(fp);
		if (type < 1) {
			int type2 = foldOutService.edit_tsfps(fp);
			if (type2 == 1) {
				int type3 = foldOutService.updtsfps(fpbs,fp);
				if (type3 == 1) {
					result = ResultObject.build(String.valueOf(type3), "修改成功！");

				} else {
					result = ResultObject.build(String.valueOf(type3), "从表操作失败！");
				}
			} else {
				result = ResultObject.build(String.valueOf(type2), "主表操作失败！");
			}
		} else {
			result = ResultObject.build(String.valueOf(0), "操作失误，您输入的时间有错误！");
		}
		return SUCCESS;
	}
	

}
